#include <string.h>
#include <sys/time.h>
#include <sys/epoll.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>

#include "Public.h"
#include "Log.h"
#include "Connect.h"

namespace sevent{

Connect::Connect():_fd(-1), _status(NOTUSE), _count(0){
}

Connect::Connect(int fd, Address addr):_fd(fd), _peerAddr(addr), _status(NOTUSE), _count(0){
}

int Connect::read(void *buf, int len, int timeout){
	if(!alive())
		return -2;
	return sevent::tcpRead(_fd, buf, len, timeout);
}

int Connect::write(void *buf, int len, int timeout){
	if(!alive())
		return -2;
	return sevent::tcpWrite(_fd, buf, len ,timeout);
}

/*
int Connect::connect(){
	int fd = sevent::tcpConnect(getPeerAddr());
	if(fd < 0)
		return fd;
	_fd = fd;
	_status = READY;
	return 0;
}
*/

bool Connect::alive(){
	if(_fd == -1)
		return false;
	if(empty())
		return false;
//	if(_status == NOTUSE || _status == DEAD)
//		return false;
	return true;
}

int Connect::close(){
	_count = 0;
	sevent::tcpClose(_fd);
	_fd = -1;
}

}
